package com.tom.demo.service.system.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tom.demo.common.component.PwdEncryption;
import com.tom.demo.common.entity.SysMenu;
import com.tom.demo.common.entity.SysUser;
import com.tom.demo.common.response.Result;
import com.tom.demo.common.utils.RecursiveMenuUtil;
import com.tom.demo.dto.LoginDto;
import com.tom.demo.mapper.SysMenuMapper;
import com.tom.demo.mapper.SysRoleMapper;
import com.tom.demo.mapper.SysUserMapper;
import com.tom.demo.service.system.SysUserService;
import com.tom.demo.vo.PermissionsVo;
import com.tom.demo.vo.RouterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private PwdEncryption pwdEncryption;

    @Override
    public Result login(LoginDto loginDto) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername,loginDto.getUsername());
        lambdaQueryWrapper.eq(SysUser::getStatus,0);
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
        if(Optional.ofNullable(sysUser).isPresent()){
            //获取到明文密文密码
            String ciphertext = sysUser.getPassword();
            if(pwdEncryption.decrypt(loginDto.getPassword(), ciphertext)){
                StpUtil.login(sysUser.getUserId());
                return Result.success();
            }else {
                return Result.error("密码错误");
            }
        }
        return Result.error("用户名不存在");
    }

    @Override
    public Result getUserInfo() {
        String userId = StpUtil.getLoginIdAsString();
        SysUser sysUser = sysUserMapper.selectById(userId);
        return Result.success(sysUser);
    }

    @Override
    public Result getPermission() {
        String userId = StpUtil.getLoginIdAsString();
        PermissionsVo permissionsVo = new PermissionsVo();
        //查询角色权限和按钮权限
        if("1".equals(userId)){
            Set<String> roleKeys = sysRoleMapper.findByRoleKey(null);
            permissionsVo.setRoles(roleKeys);
            Set<String> permissions = sysMenuMapper.findByPerms(null);
            permissionsVo.setPermissions(permissions);
        }else {
            Set<String> roleKeys = sysRoleMapper.findByRoleKey(userId);
            permissionsVo.setRoles(roleKeys);
            Set<String> permissions = sysMenuMapper.findByPerms(userId);
            permissionsVo.setPermissions(permissions);
        }
        return Result.success(permissionsVo);
    }

    @Override
    public List<RouterVo> getRouters() {
        String userId = StpUtil.getLoginIdAsString();
        //查询菜单
        List<SysMenu> sysMenuList;
        if("1".equals(userId)){
            //查询的是M和C
            sysMenuList = sysMenuMapper.findByAll();
        }else {
            //查询的是M和C
            sysMenuList = sysMenuMapper.findByUserId(userId);
        }
        if(!CollectionUtils.isEmpty(sysMenuList)){
            //构建前段需要的路由结构
            List<SysMenu> sysMenus = RecursiveMenuUtil.buildMenuTree(sysMenuList);
            return RecursiveMenuUtil.buildRouter(sysMenus);
        }
        return null;
    }
}
